package com.mulanweb.api.module.controller.admin;

import com.jfinal.kit.HashKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.mulanweb.api.module.controller.BaseController;
import com.mulanweb.api.module.model.AdminUser;
import com.mulanweb.api.module.model.User;
import com.mulanweb.db.common.Constant;
import com.mulanweb.db.common.SQLGenerateHelper;
import com.mulanweb.handler.PostHandler;
import com.mulanweb.handler.wechat.TokenHandler;
import com.mulanweb.validator.annotation.ValidateFile;
import com.mulanweb.validator.annotation.ValidateForm;

import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/13.
 */
@ValidateFile
public class AdminUserController extends BaseController {

    @ValidateForm
    public void search(){
        SQLGenerateHelper helper = new SQLGenerateHelper("admin_user","create_time  desc");
        helper.addEqualsCondition("username",getPara("username"));
        helper.addEqualsCondition("mobile",getPara("mobile"));
        helper.addIsNullCondition("delete_time");
        helper.addColumn("id");
        helper.addColumn("username");
        helper.addColumn("create_time");
        helper.addColumn("mobile");
        Page<AdminUser> page = AdminUser.dao.paginate(getPageNumber(),getPageSize(),helper.getSqlSelect(),helper.getSqlExceptSelect(),helper.getQueryParas());
        returnPageModelJson(page);
    }

    @ValidateForm
    public void save(){
        Integer adminId = super.getSessionAttr("adminId");
        if(adminId == 1){
            String username = getPara("username");
            String password = getPara("password");
            AdminUser adminUser = AdminUser.dao.findFirst("SELECT * FROM admin_user WHERE username = ?",username);
            if(StrKit.notNull(adminUser)){
                returnJson(0,"用户已存在");
            }else{
                adminUser = new AdminUser();
                adminUser.setPassword(HashKit.md5("mulan_xxl&admin_login"+password));
                adminUser.setUsername(username);
                adminUser.setMobile(getPara("mobile"));
                adminUser.setCreateTime(new Date());
                if(adminUser.save()){
                    returnSuccessJson();
                }else{
                    returnFailureJson();
                }
            }
        }else{
            returnJson(0,"无权限进行此操作");
        }
    }
    @ValidateForm
    public void update(){
        Integer loginAdminId = getSessionAttr("adminId");
        Integer adminId = getParaToInt("adminId");
        if(loginAdminId == 1 || (adminId == loginAdminId && loginAdminId != null)){
            AdminUser adminUser = AdminUser.dao.findById(adminId);
            adminUser.setPassword(HashKit.md5("mulan_xxl&admin_login"+getPara("password")));
            adminUser.setMobile(getPara("mobile"));
            if(adminUser.update()){
                returnSuccessJson();
            }else{
                returnFailureJson();
            }
        }else{
            returnJson(0,"无权限进行此操作");
        }
    }

    @ValidateForm
    public void delete(){
        Integer loginAdminId = getSessionAttr("adminId");
        Integer adminId = getParaToInt("adminId");
        if(loginAdminId == 1 || (adminId == loginAdminId && loginAdminId != null)){
            AdminUser adminUser = AdminUser.dao.findById(adminId);
            adminUser.setDeleteTime(new Date());
            if(adminUser.update()){
                returnSuccessJson();
            }else{
                returnFailureJson();
            }
        }else{
            returnJson(0,"无权限进行此操作");
        }
    }
}
